Passed
Push — master ( 02f199...7bf44b )
by Night
56s
created

stringFuncs.splitWords   F

Complexity

Conditions 18
Paths 11

Size

Total Lines 69
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 3 Features 0
Metric Value
cc 18
eloc 36
c 4
b 3
f 0
nc 11
nop 1
dl 0
loc 69
rs 1.2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like stringFuncs.splitWords often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
/** global: UB */
2
3
UB.wordSeperators = [" ", ".", ",", ";", "[", "]", "{", "}", "(", ")"];
4
5
6
7
var arrayFuncs = {
8
9
	joinWords: function(){
10
		var words = this;
11
		return words.join(" ");
12
	},
13
14
none:null
15
};
16
17
// register funcs
18
UB.registerFuncs(Array.prototype, arrayFuncs);
19
20
21
22
var stringFuncs = {
23
	
24
	/** advanced: splits by any operator or number char, space & newline.
25
	 * simple: splits by newline & space.
26
	 * always returns an array of non-blank, trimmed words. */
27
	splitWords: function(advanced = true){
28
		var text = this;
29
30
		if (advanced){
31
			
32
			var results = [];
33
			
34
			// find start of word
35
			for (var c = 0, cl = text.length;c<cl;c++){
36
				var cc = text.charCodeAt(c);
37
				var start = c;
38
				
39
				if (Chars.IsLetter(cc)) {
0 ignored issues
show
Bug introduced by
The variable Chars seems to be never declared. If this is a global, consider adding a /** global: Chars */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
40
					
41
					// find end of word
42
					for (var e = c + 1;e<cl;e++){
43
						var ec = text.charCodeAt(e);
44
						var isLastChar = (e == (cl - 1));
45
						
46
						if ((!Chars.IsLetter(ec) && ec != UB.charCodes.Space) || isLastChar){
47
							
48
							// skip if "letter [dash] letter"
49
							if (!isLastChar && ec == UB.charCodes.Minus && Chars.IsLetter(text.charCodeAt(e + 1))){
50
								continue;
51
							}
52
							
53
							// add word if something found
54
							var end = isLastChar?e:e-1;
55
							if (end > start) {
56
								
57
								var term = text.getRange(start, end).trim();
58
								if (S.Exists(term)){
0 ignored issues
show
Bug introduced by
The variable S seems to be never declared. If this is a global, consider adding a /** global: S */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
59
									
60
									if (splitBySpaceAlso) {
0 ignored issues
show
Best Practice introduced by
If you intend to check if the variable splitBySpaceAlso is declared in the current environment, consider using typeof splitBySpaceAlso === "undefined" instead. This is safe if the variable is not actually declared.
Loading history...
61
										A.Add(results, S.SplitWords(term));
0 ignored issues
show
Bug introduced by
The variable A seems to be never declared. If this is a global, consider adding a /** global: A */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
62
									} else {
63
										results.push(term);
64
									}
65
								}
66
							}
67
							
68
							// continue at ending
69
							c = e;
1 ignored issue
show
Complexity Coding Style introduced by
You seem to be assigning a new value to the loop variable c here. Please check if this was indeed your intention. Even if it was, consider using another kind of loop instead.
Loading history...
70
							break;
71
						}
72
					}
73
				}
74
			}
75
			return results;
76
77
		}else{
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
78
79
			// SPLIT BY NEWLINE & SPACES
80
			results = [];
81
			var lines = text.splitLines(true, true);
82
			for (var s = 0, sl = lines.length; s < sl; s++) {
83
				var line = lines[s];
84
				results = line.split(" ");
85
				for (var w = 0, wl = results.length; w < wl; w++) {
86
					var word = results[w];
87
					if (word.length > 0) {
88
						results.push(word);
89
					}
90
				}
91
			}
92
			return results;
93
			
94
		}
95
	},
96
	
97
	/** Count words in a string */
98
	wordCount: function(){
99
		var text = this;
100
		return text.match(new RegExp("\\b\\w+\\b", "g")).length;
101
	},
102
	none:null
103
};
104
105
// register funcs
106
UB.registerFuncs(String.prototype, stringFuncs);